/*Variable = {
    sellerDetailTag:true,
}*/
//更多商品的提示
//var moreCommodity = true;
App = {
    showLoading:function(){
        myApp.showPreloader();
    },
    hideLoading:function(){
        myApp.hidePreloader();
    },

    //判断类型为门票还是包车
    judgeType:function(typeId){
        var ticketTypeId = [1,2,3,23];
        var charteredTypeId = [6,7,8,9,10,24];
        if(ticketTypeId.join(",").indexOf(typeId) !=-1){
            typeId = 13;//车陪游图片
        }else if(charteredTypeId.join(",").indexOf(typeId) !=-1){
            typeId = 15;//包车陪游图片
        }
        return typeId;
    },

    //加载商家商品列表页
    loadSellerShop:function(){
        App.showLoading();
        var typeId = App.judgeType($("#detail-typeId").val());
        var targetId = $("#detail-targetId").val();
        var url = Constant.ctx('/mulanweb/common/seller/searchContent');
        if(typeId == 23 || typeId == 13){
            typeId = 23;
        }else if(typeId == 24 || typeId==15){
            typeId = 24;
        }
        var para = {
            typeId:typeId,
            targetId:targetId,
        }
        $.post(url,para,function(info){
            if(info.list){
                var data = {
                    DataList:info.list
                }
                var moduleEvaluatesHtml = $("#contentSearchDataList").html();//模板对象
                var compiledTemplate = Template7.compile(moduleEvaluatesHtml);
                var newEvaluatesHtml = compiledTemplate(data);
                $("#seller-queryResultList").append(newEvaluatesHtml);
                $$('.infinite-scroll-preloader').remove();
            }
            console.info(info);
            App.hideLoading();
        })
    },

    //加载详情页
    loadDetailPage:function() {
        //加载更多商品提示
      /*  if(moreCommodity){
            Constant.alertNotification("更多商品请点击右上角");
            moreCommodity = false;
        }*/
        App.showSellerShop();
        var targetId = $("#detail-targetId").val();
        var typeId = App.judgeType( $("#detail-typeId").val());
        var para = {
            typeId: typeId,
            targetId: targetId
        }
        App.getPhoto(para);
        var queryUrl = "";
        if(typeId == 23 || typeId ==13){
            queryUrl=Constant.ctx('/mulanweb/common/ticket/getDetail');
        }else {
            queryUrl=Constant.ctx("/mulanweb/common/chartered/getDetail");
        }
        $.post(queryUrl, para, function (info) {
            if (info.ReturnCode == 1) {
                var data = info.DataList.Target;
                data.Distance = info.DataList.Distance;
                data.DescriptionTag = true;
                if(data.CpTd || data.XcAp || data.FySm || data.YdXz || data.ZySx || data.TgGz){
                    data.DescriptionTag = false;
                }
                var moduleDetailHtml = $("#contentDetailModule").html();
                var compiledTemplate = Template7.compile(moduleDetailHtml);
                var newDetailHtml = compiledTemplate(data);
                $("#ticket-detail").append(newDetailHtml);
                $("#ticket-score").html(info.DataList.Score);
            }
        });

        var evaluatePara = {
            targetId: targetId,
            typeId: 23,
            PageSize: 10,
            PageNumber: 1
        }
        App.getEvaluates(evaluatePara)//添加评论列表
        App.hideLoading();
    },

    //当typeTag存在则表示查询用户是否收藏此内容，否则执行保存或删除收藏操作
    userCollection:function(typeId,typeTag) {
        var para = {
            typeId: typeId,//24表示门票
            targetId:$("#detail-targetId").val() ,
            typeTag: typeTag
        }
        $.post(Constant.ctx("/mulanweb/common/collection/saveOrQuery"), para, function (info) {
            if (info.ReturnCode == 1) {
                var tt = $("#collection-img").attr("src");
                if (info.AlertMessage == "1") {
                    $("#collection-img").attr("src", $("#collection-img").attr("src").replace("false", "true"));
                } else if (info.AlertMessage == "2") {
                    $("#collection-img").attr("src", tt.replace("true", "false"));
                }
            }else if(info.ReturnCode == 0 && !typeTag){
                Constant.alert(info.AlertMessage)
            }
        })
    },

    //查询商家信息
    requestSellerInfo:function (typeName) {
        var para = {
            typeName: typeName,
            typeId: $("#detail-typeId").val(),
            targetId: $("#detail-targetId").val()
        }
        $.post(Constant.ctx("/mulanweb/common/seller/requestInfo"), para, function (info) {
            Constant.alertMessage(info.ReturnStr,info.AlertMessage);
        });
    },

    //查询签证信息
    requestVisaInfo:function (typeName) {
        var para = {
            typeName: typeName,
            targetId: $("#detail-targetId").val()
        }
        $.post(Constant.ctx("/mulanweb/common/seller/requestVisaInfo"), para, function (info) {
            Constant.alertMessage(info.ReturnStr,info.AlertMessage);
        });
    },

    //查询内容评论
    getEvaluates:function (evaluatePara,evaluateModule,evaluateTargetModule){
        var module = evaluateModule  || $("#ticketEvaluateModule");
        var target = evaluateTargetModule || $("#detail-evaluates-total");
        $.post(Constant.ctx('/mulanweb/evaluate/getEvaluates'), evaluatePara, function (info) {
            if (info.ReturnCode == 1) {
                var data = info.DataList;
                for(var rr in data){
                    for(var vv in data[rr]){
                        if(vv == "Score"){
                            var score = data[rr][vv];
                            var i = 0 ;
                            var img =[] ;
                            while(i<(score/2)){
                                img.push('<img src="'+Constant.cdnContentPath+'/static/image/detail/collection-1x-true.png"/>');
                                i++;
                            }
                            i=img.length;
                            while(i<5){
                                img.push('<img src="'+Constant.cdnContentPath+'/static/image/detail/collection-1x-false.png"/>');
                                i++;
                            }
                            data[rr][vv]=img.join("");
                        }else if(vv == "CreateTime"){
                            var createTime = data[rr][vv];
                            if(createTime){
                                createTime= createTime.split(" ")[0];
                                data[rr][vv]=createTime;
                            }
                        }
                    }
                }
                var moduleEvaluatesHtml = module.html();//模板对象
                var compiledTemplate = Template7.compile(moduleEvaluatesHtml);
                var newEvaluatesHtml = compiledTemplate(info);
                $("#evaluateContent").append(newEvaluatesHtml);
                //设置评论数
                target.html(info.TotalRow);
            }
        });
    },

    //得到并加载轮播图
    getPhoto:function (para){
            $.post(Constant.ctx('/mulanweb/common/photo/getPhotos'), para, function (data) {
                var html = '';
                if (data.ReturnCode == 1) {
                    var info = data.DataList;
                    for (var i = 0; i < info.length; i++) {
                        html += '<li><img src="'+Constant.photoPath+ info[i] + '" width="100%" height="200"/></li>';
                    }
                }
                $("#banner").append(html);
                $('.ml-banner').unslider({
                    speed: 1000,               //  The speed to animate each slide (in milliseconds)
                    delay: 5000,                //  The delay between slide animations (in milliseconds)
                    complete: function () {
                    },    //  A function that gets called after every slide animation
                    fluid: false                //  Support responsive design. May break non-responsive designs
                });
                var unslider04 = $('.ml-banner').unslider({
                        dots: true,
                        keys: true,                 //  Enable keyboard (left, right) arrow shortcuts
                    }),
                    data04 = unslider04.data('unslider');
                $('.unslider-arrow04').click(function() {
                    var fn = this.className.split(' ')[1];
                    data04[fn]();
                });
            });
        },

    //无搜索页使用此控制菜单显示与否
    pageAfterAnimation:function(pageName){
        if  (pageName === 'index'){
            $("#index-tool").html("").removeClass("toolbar");
            App.hideSellerShop();
            myApp.hideToolbar();
        }else if(pageName === 'seller-shop'){
            $("#index-tool").html("").removeClass("toolbar");
            myApp.hideToolbar();
            App.hideSellerShop();
        }else if(pageName === 'detail') {
            App.detailBottomMenu();
        }
    },

    //有搜索页使用此控制菜单显示与否
    pageAfterAnimation2:function(pageName){
        if (pageName === 'index') {
            var toolbarHtml = '<div class="toolbar-inner" style="background-color:white;">' +
                '     <a class="button" id="toolbar-button" onclick="handler()" style="width:60%;margin-left: 20%;background-color:#509AF1;color:white;height:40px;line-height:40px;border-radius:2px;">搜索</a>' +
                '</div>';
            $("#index-tool").html(toolbarHtml);
            mainView.showToolbar();
        } else if (pageName === 'search') {
            mainView.hideToolbar();
            App.hideSellerShop();
        }else if(pageName === 'seller-shop'){
            $("#index-tool").html("").removeClass("toolbar");
            myApp.hideToolbar();
            App.hideSellerShop();
        } else if (pageName === 'detail') {
            App.detailBottomMenu();
        }
    },

    //加载门票预定页面
    loadTicketReservePage:function(){
        $(".tagHeight").click(function(){
            var current = $(this).height();
            var amount = $(this).prevAll().length - 1;
            $(".page-content").scrollTop(current * amount);
        });
        $(".tagHeight2").click(function(){
            var current = $(this).height();
            $(".page-content").scrollTop(current * 4);
        });


        var toolbarHtml = '<div class="toolbar-inner" style="text-align: center;padding:0px">' +
            '<a style="font-size: 16px;color:#343434;margin-left:1%">总额：<label id="orderAmount" style="color:red">0</label>元</a>' +
            '<a id="ticket-reserve" style="width:45%;height:60px;background-color: #509AF1;line-height: 60px;color: #FFFFFF;">现在预定</a>' +
            '</div>';
        $("#index-tool").html(toolbarHtml);

        $("#reserve-ticket-amount").on("change", function () {
            var tt = $("#reserve-ticket-amount").val() * ($("#ticket-detail-price").html().replace("￥", ""));
            $("#orderAmount").html(tt);
        })

        /*var path = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx1a5d74a8c7cb135e&redirect_uri=http%3a%2f%2f15z6868x01.51mypc.cn%2fmulan_xxl%2fadmin%2ftest%2fgetAuthority&response_type=code&scope=snsapi_base&state=123#wechat_redirect";*/
        $("#ticket-reserve").on("click", function () {
            App.showLoading();
            var amount = $("#reserve-ticket-amount").val();
            var mobile = $("#areaCode").val()+$("#reserve-ticket-mobile").val();
            var remarks = $("#reserve-ticket-remarks").val();
            var couponCode = $("#reserve-ticket-coupon").val();
            var ticketId = $("#detail-targetId").val();
            var para = {
                amount: amount,
                mobileNumber: mobile,
                couponCode: couponCode,
                targetId: ticketId,
                typeId: 23,
                remarks: remarks
            }
            $.post(Constant.contextUrl+'/mulanweb/wechat/order/save', para, function (info) {
                if (info.message || info.AlertMessage) {
                    Constant.alert(info.message || info.AlertMessage);
                }
                if (info.returnCode == 1) {
                    var target = info.target;
                    window.location.href=Constant.contextUrl+"/view/wxpay/confirm_pay.jsp?price=" + info.lastPrice + "&title=" + target.title + "&targetLogo=" + info.targetLogo + "&orderNumber=" + info.orderNumber;
                }
                App.hideLoading();
            });
        });
    },

    //加载包车预定页面
    loadCharteredReservePage:function(typeId){
        //var amount = $("#detail-amount").val();
        
        var amount = 1;
        var danwei = '天';
        if(typeId == 9 ){
            danwei = "小时";
        }else if(typeId==10){
            danwei = "次";
        }
        var tt = $("#ticket-detail-price").html().replace("￥", "") * amount;
        var toolbarHtml = '<div class="toolbar-inner" style="text-align: center;padding:0px">' +
            '<a style="font-size: 16px;color:#343434;margin-left:1%">总额：<label id="orderAmount" style="color:red">'+tt+'</label>元（'+amount+danwei+'）</a>' +
            '<a id="ticket-reserve" style="width:45%;height:60px;background-color: #509AF1;line-height: 60px;color: #FFFFFF;">现在预定</a>' +
            '</div>';
        $("#index-tool").html(toolbarHtml);
        $(".charter-info").html(newToolbar(toolbarPara));

        $("#ticket-reserve").on("click", function () {
            App.showLoading();
            var mobile = $("#areaCode").val()+ $("#reserve-ticket-mobile").val();
            var remarks = $("#reserve-ticket-remarks").val();
            var couponCode = $("#reserve-ticket-coupon").val();
            var targetId = $("#detail-targetId").val();
            var para = {
                amount: amount,
                mobileNumber: mobile,
                couponCode: couponCode,
                targetId: targetId,
                startTime:toolbarPara.startTime,
                endTime:toolbarPara.endTime,
                typeId: 24,
                remarks: remarks
            }
            $.post(Constant.ctx('/mulanweb/wechat/order/save'), para, function (info) {
                if (info.message || info.AlertMessage) {
                    Constant.alertMessage(info.message , info.AlertMessage);
                }
                if (info.returnCode == 1) {
                    var target = info.target;
                    window.location.href = Constant.ctx("/view/wxpay/confirm_pay.jsp?price=" + target.price + "&title=" + target.title + "&targetLogo=" + info.targetLogo + "&orderNumber=" + info.orderNumber);
                }
                App.hideLoading();
            });
        })
    },

    //详情页底部菜单栏
    detailBottomMenu:function(){
        var typeId= App.judgeType($("#detail-typeId").val());
        typeId = typeId==13?23:24;
        var toolbarHtml = '<div class="toolbar-inner" style="text-align: center;padding:0px">' +
            '<a style="width:20%;" onclick="App.requestSellerInfo(\'email\')"><img src="'+Constant.cdnContentPath+'/static/image/detail/email-2x.png" height="25"/></a>' +
            '<a style="width:20%;" onclick="App.requestSellerInfo(\'mobile\')"><img src="'+Constant.cdnContentPath+'/static/image/detail/phone-2x.png" height="25"/></a>' +
            '<a style="width:20%;" onclick="App.userCollection('+typeId+')"><img id="collection-img" src="'+Constant.cdnContentPath+'/static/image/detail/collection-2x-false.png" height="25"/></a>' +
            '<a id="ticket-reserve" style="width:45%;height:60px;background-color: #509AF1;line-height: 60px;color: #FFFFFF;">现在预定</a>' +
            '</div>';
        $("#index-tool").addClass("toolbar").html(toolbarHtml);
        if(typeId == 23 ){
            $("#ticket-reserve").attr("href", "/view/menu2/ticket/reserve.jsp");
        }else if(typeId == 24){
            $("#ticket-reserve").attr("href", "/view/menu2/chartered/reserve.jsp");
        }
        $("#ticket-reserve").on("click",App.hideSellerShop);
        App.userCollection(typeId,'query');
        mainView.showToolbar();
        App.showSellerShop();
    },

    //控制搜索页选择城市空间居中显示
    cityInputCenter:function(){
        var userAgent = navigator.userAgent;
        if (userAgent.indexOf("Firefox") != -1 || userAgent.indexOf("Presto") != -1) {
            //表示浏览器为火狐浏览器，不进行任何操作
        } else {
            var newV =  $("#cityId").find("option:selected").text();
            if(newV){
                $("#span").html(newV);
            }
            var fontWidth = $("#span").width();
            var selectWidth = $("#cityId").width();
            $("#cityId").css("text-indent", (selectWidth - fontWidth) / 2 - 10);
        }
    },

    showSellerShop:function(){
        $("#sellerShop").html('<a class="link" href="/view/menu2/seller-shop.jsp" style="color: #007aff;"><span>更多商品</span></a>');
    },
    hideSellerShop:function(){
        $("#sellerShop").html('');
    },

    loadTitle:function(title){
        $("title").html(title);
    }
}



